<?php
class Back_DroitsController extends Osml_Controller_Admin_Action
{

    public function indexAction()
    {
        // action body
    }

    public function autorisationsAction()
    {
        $model = new Back_Model_Droits();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_droits';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
            'id_droits' => array(
                'name' => 'id', 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'id_role' => array(
                'name' => 'role', 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            'ressource' => array(
                'name' => __("ressource"), 
                'link' => array(
                    'action' => 'editer'
                )
            ), 
            "autorise" => __("Possible"), 
            "action" => __("Action"), 
            "creation_utilisateur" => __("Crée par"), 
            "creation_date" => __("Crée le")
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
            'callback' => array(
                $this, 
                '_transformRowsCallback'
            ),  // Transformation callback
            'link' => array(
                'action' => 'voir'
            ), 
            'pageNumber' => (int) $this->_getParam('page', 1), 
            'pageItemPerPage' => (int) $this->_getParam('par', 10), 
            'form' => $form
        );
    }

    public function editerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        // Préparation de la liste des ressources
        $model = new Back_Model_Ressources();
        $ressources = $model->lister("module");
        $liste[''] = __("Choisir une ressources");
        foreach ($ressources as $ressource) {
            $courrantModule = $ressource->getModule();
            $nomRessource = $courrantModule . "." . $ressource->getNom();
            $liste[$courrantModule][$nomRessource] = $nomRessource;
        }
        $options['ressources'] = $liste;
        
        // Préparation de la liste des roles
        $model = new Back_Model_Roles();
        $roles = $model->listerPourSelect(true, "nom", "id");
        $roles[""] = __("Choisir un rôle");
        $options['roles'] = $roles;
        
        $form = new Back_Form_Droits('Formulairejndpro', $options);
        $model = new Back_Model_Droits($id);
        
        if ($requete->isPost()) {
            if ($form->isValid($requete->getPost())) {
                $date = date(Jnd_Global_Constantes::FORMAT_DATE_BASE);
                if (intval($id) > 0) {
                    $model->charger($id);
                    $model->setOptions($form->getValues());
                } else {
                    $model->setOptions($form->getValues());
                }
                $resultat = $model->sauver();
                $id = $model->getId();
                
                $this->redirect("autorisations");
            }
        } elseif ($requete->isGet()) {
            if ($id > 0) {
                $model->charger($id);
                
                $ressource = $model->getRessource();
                $listeAction = $this->_recuperationActions($ressource);
                $form->getElement("action")->setMultiOptions($listeAction);
                
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
        $this->view->model = $model;
    }

    public function supprimerAction()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $model = new Back_Model_Droits($id);
        $model->supprimer($id);
        
        $this->redirect("autorisations");
    }

    /**
     *
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback( $row, $view )
    {
        if (isset($row["creation_utilisateur"])) {
            $row['creation_utilisateur'] = Back_Model_Utilisateurs::pseudoParId($row['creation_utilisateur']);
            ;
        }
        
        if (isset($row["id_role"]) && intval($row["id_role"])) {
            $row["id_role"] = Back_Model_Roles::recuperationValeurParId($row['id_role']);
        }
    }

    protected function _recuperationActions( $ressource )
    {
        $request = $this->getRequest();
        list ($module, $controllerNom) = explode(".", $ressource);
        $tmp = ucfirst($controllerNom) . ucfirst($request->getControllerKey());
        if ($module == "front" || $module == "default") {
            $classe = $tmp;
        } else {
            $classe = ucfirst($module) . "_" . $tmp;
        }
        $new = realpath(APPLICATION_PATH . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR . "$module" . DIRECTORY_SEPARATOR . "controllers" . DIRECTORY_SEPARATOR . $tmp . ".php");
        require_once $new;
        $test = new $classe($request, $this->getResponse());
        $reflection = new ReflectionClass($classe);
        $actions["*"] = __("Toutes");
        foreach ($reflection->getMethods() as $methode) {
            if (strpos($methode->getName(), "Action") !== false && substr($methode->getName(), 0,1) != "_") {
                $actionNom = preg_replace("/Action/", "", $methode->getName());
                $actions[$actionNom] = ucfirst($actionNom);
            }
        }
        
        return $actions;
    }
}

